import datetime

from db.dao import OrderDao
from quantification.data_handler.Account import Account
from quantification.data_handler.DealRecord import DealRecord
from quantification.data_handler.Order import Order
from quantification.data_handler.PositionRecord import PositionRecord
from quantification.deal_handler.DealHandler import DealHandler
from quantification.data_handler.Position import Position
from utils.CommonEnum import DealDirectionEnum, OrderStateEnum


class DemoDealHandler(DealHandler):

    def __init__(self, stock_code):
        self.stock_code = stock_code
        self.account = Account()

    def buy(self,price):
        # 买入信号
        # 获取可用资金（买入时判断）
        available_funds = self.account.remaining_available_funds
        if available_funds <= 0:
            # 无可用资金,不用操作
            return

        # 下订单
        stock_count = int(available_funds / price / 100) * 100
        new_order = Order(self.stock_code, None, DealDirectionEnum.BUY.code, stock_count,
                          price)
        new_order.store_data()

        # 更新持仓静态数据
        position = Position(self.stock_code, None)

        quantity = position.hold_quantity
        if quantity == 0:
            position.buy_time = datetime.datetime.now()
            position.hold_quantity = stock_count
            position.average_price = price
            record = PositionRecord(position=position, order=new_order)
        else:
            position.hold_quantity = quantity + stock_count
            record = PositionRecord(position=position, order=new_order)
            position.add_position_record(record)
            position.calculate_average_price()

        position.store_data()

        # 新增持仓历史记录
        record.store_data()

        #    计算账户金额
        self.account.calculate_account_funds(record)
        self.account.store_data()

    #     更新订单状态为已完成
        new_order.change_order_state(OrderStateEnum.COMPLETED.code)


    def sell(self,price):

        # 卖出信号
        # 股票持仓情况
        position = Position(self.stock_code, None)
        stock_position = position.hold_quantity
        if stock_position <= 0:
            # 没有持仓,不用操作
            print('当前没有持仓，不需操作')
            return

        # 查看当天有没有相同的未完成的订单，如果有先不重复下单
        # todo 复杂场景：分价位设置不同的多份订单，本次只简单实现
        orders = OrderDao().queryEffectiveOrder(self.stock_code)
        if orders:
            print('该股票当天已经下过单，不需要重复下单')
            return
        # 下订单
        new_order = Order(self.stock_code, position.stock_name, DealDirectionEnum.SELL.code, stock_position,
                          price)
        new_order.store_data()
        # 更新持仓静态数据
        position.hold_quantity = 0
        position.store_data()
        # 新增持仓历史记录
        record = PositionRecord(position=position, order=new_order)
        record.store_data()
        #    计算账户金额
        self.account.calculate_account_funds(record)

        self.account.store_data()
        #     更新订单状态为已完成
        new_order.change_order_state(OrderStateEnum.COMPLETED.code)


